package valuecomusa.fijibus.eticket;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;



public class SunbeamWS {

	//public static String URL = "http://192.168.100.33/ROI/AndroidWS/sunbeam.asmx";
	
	public static String URL = "http://webservice3.trackmyroi.com/androidws/sunbeam.asmx";
	public static String SessionID="";
	private static final String NAMESPACE = "http://tempuri.org/";
	private static String SpecialChar = "»";
	
	public String ErrMsg;
	public Activity act;
	
	public SunbeamWS(Activity parent){
		act=parent;
	}
	
	public SunbeamWS() {
	}
	
	public int AddTrans(int MemberISN, String TranID, String TranDate,
			int CheckInPayType, double FareAmount, double TopUpAmount,
			String Desc, int BusISN, int ReceiverISN)
	{
		try {
			SoapObject request = new SoapObject(NAMESPACE, "CheckIn"); 
			
			request.addProperty("Key", SessionID);  
			request.addProperty("MemberISN", MemberISN);
			request.addProperty("TranID", TranID);
			request.addProperty("TranDate", TranDate);
			request.addProperty("CheckInPayType", CheckInPayType);
			request.addProperty("FareAmount", String.valueOf(FareAmount));
			request.addProperty("TopUpAmount", String.valueOf(TopUpAmount));
			request.addProperty("Desc", Desc);
			request.addProperty("BusISN", BusISN);
			request.addProperty("ReceiverISN", ReceiverISN);
			
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
			envelope.dotNet=true;
			
			envelope.setOutputSoapObject(request);
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
			
			androidHttpTransport.call("http://tempuri.org/CheckIn", envelope);
		  
		    SoapPrimitive  resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
		    
		    String []items=resultsRequestSOAP.toString().split(SpecialChar);
		    
		    ErrMsg="";
		    int result=Integer.parseInt(items[0]);
		    if(result>=0) // success
		    	return Integer.parseInt(items[1]);
		    
		    ErrMsg=items[1];
		    return result;
		   
		} catch (Exception e) {
			e.printStackTrace();
			ErrMsg=e.getMessage();
			return -1;
		}
	}
	
	public ArrayList<Customer> GetCustomers(int mTranISN, int mMemberISN, int ItemPage, int CurPage, String SortBy, String SortDirect)
	{
		
		try {
			SoapObject request = new SoapObject(NAMESPACE, "GetCustomers"); 
	
			request.addProperty("Key", SessionID);  
			request.addProperty("mTranISN", mTranISN);
			request.addProperty("mMemberISN", mMemberISN);
			request.addProperty("ItemPage", ItemPage);  
			request.addProperty("CurPage", CurPage);
			request.addProperty("SortBy", SortBy);  
			request.addProperty("SortDirect", SortDirect);
			
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
			envelope.dotNet=true;
			
			envelope.setOutputSoapObject(request);
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
			
			androidHttpTransport.call("http://tempuri.org/GetCustomers", envelope);
		  
		    SoapPrimitive  resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
		    
		    String []items=resultsRequestSOAP.toString().split(SpecialChar);
		    
		    ArrayList<Customer> custs=new ArrayList<Customer>();
		    ErrMsg="";
		    if(items[0].equals("0")) // success
		    {
		    	JSONArray entries = new JSONArray(items[1]);
		    	for (int i=0; i<entries.length(); i++)
	            {
	                JSONObject post = entries.getJSONObject(i);
	                Customer cust=new Customer();
	                cust.setID( post.getInt("MemberISN") );
	                cust.setUsername(post.getString("memUserName"));
	                cust.setFirstName( post.getString("memFirstName") );
	                cust.setLastName( post.getString("memLastName") );
	                cust.setPhoneNumber( post.getString("memPhone") );
	                cust.setBalance( post.getDouble("memBalance") );
	               
	                custs.add(cust);
	            }
		    	return custs;
		    }
		    ErrMsg=items[1];
		    return null;
		   
		 
		} catch (Exception e) {
			e.printStackTrace();
			ErrMsg=e.getMessage();
			return null;
		}
	}
	
	public int[] GetLastISN()
	{
		
		try {
			SoapObject request = new SoapObject(NAMESPACE, "GetLastISN"); 
	
			request.addProperty("Key", SessionID);  

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
			envelope.dotNet=true;
			
			envelope.setOutputSoapObject(request);
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
			
			androidHttpTransport.call("http://tempuri.org/GetLastISN", envelope);
		  
		    SoapPrimitive  resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
		    
		    String []items=resultsRequestSOAP.toString().split(SpecialChar);
		    
		    int tmp[]=new int[2];
		    ErrMsg="";
		    if(items[0].equals("0")) // success
		    {
		    	tmp[0] = Integer.parseInt(items[1]);
		    	tmp[1] = Integer.parseInt(items[2]);
		    	
		    	return tmp;
		    }
		    
		    ErrMsg=items[1];
		    return null;
		   
		 
		} catch (Exception e) {
			e.printStackTrace();
			ErrMsg=e.getMessage();
			return null;
		}
	}
}
